package top.hmtools.eureka.client.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
import org.springframework.web.servlet.view.xml.MappingJackson2XmlView;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;

@Configuration
public class ContentNegotiationConfiguration implements WebMvcConfigurer {
	
	private final Logger logger = LoggerFactory.getLogger(ContentNegotiationConfiguration.class);
	
	/**
	 * spring-boot-starter-freemarker自动实例化的freemarker视图解析器
	 * <br>与 内容协商 视图解析器是同等地位
	 * <br>内容协商 视图解析器优先级比本视图解析器高，当前者解析不了的时候，会移交给后者尝试解析，类似过滤器链。
	 */
	@Autowired
	private FreeMarkerViewResolver freeMarkerView;
	
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
//		WebMvcConfigurer.super.configureViewResolvers(registry);
		
		//xml视图
		MappingJackson2XmlView xmlView = new MappingJackson2XmlView();
		xmlView.setPrettyPrint(true);
		this.logger.info("已添加XML视图：{}",xmlView);
		
		//json视图
		FastJsonJsonView jsonView = new FastJsonJsonView();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue,SerializerFeature.NotWriteRootClassName);//如果值为null，仍然输出
		jsonView.setFastJsonConfig(fastJsonConfig);
		this.logger.info("已添加json视图：{}",jsonView);
		
		registry.enableContentNegotiation(jsonView,xmlView);
		registry.viewResolver(freeMarkerView);
		
		this.logger.info("已设置视图解析器：{}",registry);
	}
	
	/**
	 * 内容协商 使用后缀名时，需要使用此方法开启
	 * @param configurer
	 */
	@Override
	public void configurePathMatch(PathMatchConfigurer configurer) {
		configurer.setUseRegisteredSuffixPatternMatch(true).setUseSuffixPatternMatch(true);
		this.logger.info("已设置url路径匹配规则：{}",configurer);
	}

	@Override
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
		configurer
		.defaultContentType(MediaType.APPLICATION_JSON)
		.favorParameter(true)
		.parameterName("mime")
		.favorPathExtension(true)
		.ignoreUnknownPathExtensions(false)
		.ignoreAcceptHeader(false)
		.useRegisteredExtensionsOnly(true)
		.mediaType("json", MediaType.APPLICATION_JSON)
		.mediaType("hm", MediaType.APPLICATION_JSON)
		.mediaType("xml", MediaType.APPLICATION_XML)
		.mediaType("html", MediaType.TEXT_HTML)
		.mediaType("ftl", MediaType.TEXT_HTML)
//		.mediaType("pdf", MediaType.APPLICATION_PDF)
		;
		this.logger.info("已设置内容协商：{}",configurer);
	}
}
